/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui;

import java.awt.BorderLayout;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.gui.table.RowNumberComponent;
import org.tiberiumlabs.lailaps.gui.table.Table;
import org.tiberiumlabs.lailaps.gui.table.TableCellRenderer;
import org.tiberiumlabs.lailaps.gui.table.TableColumnsModel;
import org.tiberiumlabs.lailaps.gui.table.TableModel;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class GuiBuilder {

    private final Log LOG = LogFactory.getLog(GuiBuilder.class);

    // <editor-fold defaultstate="collapsed" desc=" components cache ">

    private Map<String, SoftReference<JComponent>> componentsCache = new HashMap<String, SoftReference<JComponent>>();

    private void putToCache(String key, JComponent component) {
        componentsCache.put(key, new SoftReference<JComponent>(component));
    }

    private JComponent getFromCache(String key) {
        SoftReference<JComponent> ref = componentsCache.get(key);
        if (ref != null) {
            JComponent component = ref.get();
            if (component != null) {
                return component;
            }
        }
        return null;
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" component keys for cache ">
    private static final String TALBE = "table";
    private static final String MAIN_CONTENT = "mainContent";
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" top level components ">

    private JFrame mainWindow = null;

    public boolean isMainWindowVisible() {
        return mainWindow != null;
    }

    public JFrame getMainWindow() {
        if (mainWindow == null) {
            long start = System.currentTimeMillis();

            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());

            contentPane.add(getMainContent(), BorderLayout.CENTER);

            mainWindow = new JFrame("Lailaps") {
                @Override
                public void dispose() {
                    mainWindow = null;
                    super.dispose();
                }
            };
            mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            mainWindow.addWindowListener(Mediator.getMediator().getActionFactory().getExitAction());
            mainWindow.setContentPane(contentPane);
            LOG.info("[GuiBuilder] MainWindow created in " + (System.currentTimeMillis() - start));
        }
        return mainWindow;
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" internal components ">

    public Table getTable() {
        Table table = (Table) getFromCache(TALBE);
        if (table == null) {
            table = new Table(new TableModel(Mediator.getMediator().getDownloadManager()), new TableColumnsModel());
            table.setFillsViewportHeight(true);
            table.setAutoResizeMode(Table.AUTO_RESIZE_NEXT_COLUMN);
            table.setShowGrid(false);
            table.setShowHorizontalLines(false);
            table.setShowVerticalLines(true);
            table.setDragEnabled(true);
            table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            table.setDefaultRenderer(Object.class, new TableCellRenderer());
            // TODO add mouse listener
            // TODO set default cell renderer
            // TODO set transfer handler
            putToCache(TALBE, table);
        }
        return table;
    }

    public JComponent getMainContent() {
        Box box = (Box) getFromCache(MAIN_CONTENT);
        if (box == null) {
            Table table = getTable();
            JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setRowHeaderView(new RowNumberComponent(table));
            box = Box.createVerticalBox();
            box.add(scrollPane);
            putToCache(MAIN_CONTENT, box);
        }
        return box;
    }

    // </editor-fold>

}
